//
//  XYSynButton.m
//  XYSynButton
//
//  Created by Daniel on 15-1-8.
//  Copyright (c) 2015年 Daniel. All rights reserved.
//

#import "XYOfflineButton.h"

@implementation XYOfflineButton

-(void)awakeFromNib{
    
    _backgeroundView = [[UIImageView alloc]initWithFrame:self.bounds];
    _backgeroundView.image = [UIImage imageNamed:@"offline_cloud"];
    [self addSubview:_backgeroundView];
    
    
    _statusImageView = [[UIImageView alloc]initWithFrame:self.bounds];
    _statusImageView.image = [UIImage imageNamed:@"btn_dl"];
    [self addSubview:_statusImageView];
}

-(void)setStatus:(NSInteger)status{
    
    if (_status == status) {
        return;
    }
    
    _status = status;
    
    if(status == kXYSynButtonStatusNone){
        
        _statusImageView.image = [UIImage imageNamed:@"btn_dl"];
        [self stopAnimation:_statusImageView];
    }
    else if(status == kXYSynButtonStatusDownloading){
        
        
        [self stopAnimation:_statusImageView];
        [self loadingAnimation:_statusImageView];
    }
    
    else if(status == kXYSynButtonStatusFinish){
        
        _statusImageView.image = nil;//[UIImage imageNamed:@"btn_done"];
        [self stopAnimation:_statusImageView];
        [self finishAnimation:_statusImageView];
    }
}

-(void)loadingAnimation:(UIImageView*)imageView{
    
    [UIView animateWithDuration:0.5 animations:^{
        //
        CGPoint originPoint = imageView.center;
        imageView.center = CGPointMake(originPoint.x, originPoint.y + imageView.frame.size.height/3.0);
        imageView.alpha = 0;
        
    } completion:^(BOOL finished) {
        //
        imageView.alpha = 1.0f;
        imageView.image = [UIImage imageNamed:@"btn_refresh"];
        imageView.frame = self.bounds;
        
        CABasicAnimation* fanRotationAnim = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
        fanRotationAnim.duration = 2;
        fanRotationAnim.fromValue = 0;
        fanRotationAnim.toValue = @(2 * M_PI);
        fanRotationAnim.repeatCount = CGFLOAT_MAX;
        
        [imageView.layer addAnimation:fanRotationAnim forKey:nil];
    }];
    
    
}

-(void)finishAnimation:(UIImageView*)imageView{
    
    _nikeLayer = [CAShapeLayer layer];
    _nikeLayer.frame = imageView.bounds;
    _nikeLayer.fillColor = [UIColor clearColor].CGColor;
    _nikeLayer.strokeColor = [UIColor colorWithRed:21/255.0 green:169/255.0 blue:135/255.0 alpha:1.0].CGColor;
    _nikeLayer.lineWidth = _nikeLayer.frame.size.width / 28;
    _nikeLayer.strokeEnd = 0.0;
    
    
    //based on 56 * 56
    UIBezierPath* nikePath  = [UIBezierPath bezierPath];
    [nikePath moveToPoint:CGPointMake(2 * _nikeLayer.frame.size.width / 7, 4 * _nikeLayer.frame.size.height / 7)];
    [nikePath addLineToPoint:CGPointMake(2 * _nikeLayer.frame.size.width / 7 + 2, 4 * _nikeLayer.frame.size.height / 7 - 2)];
    [nikePath addLineToPoint:CGPointMake(3 * _nikeLayer.frame.size.width / 7, 5 * _nikeLayer.frame.size.width / 8)];
    [nikePath addLineToPoint:CGPointMake(5 * _nikeLayer.frame.size.width / 8, 3 * _nikeLayer.frame.size.width / 7)];
    _nikeLayer.path = nikePath.CGPath;
    
    
    CABasicAnimation* nikeAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    nikeAnimation.delegate = self;
    nikeAnimation.duration = 0.5;
    nikeAnimation.fromValue = @0.0f;
    nikeAnimation.toValue = @1.0f;
    nikeAnimation.fillMode = kCAFillModeForwards;
    nikeAnimation.removedOnCompletion = NO;
    [_nikeLayer addAnimation:nikeAnimation forKey:nil];
    
    
    CABasicAnimation* alphaAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    alphaAnimation.duration = 0.5;
    alphaAnimation.fromValue = @0.0f;
    alphaAnimation.toValue = @1.0f;
    alphaAnimation.fillMode = kCAFillModeForwards;
    alphaAnimation.removedOnCompletion = NO;
    [_nikeLayer addAnimation:alphaAnimation forKey:nil];
    
    
    CABasicAnimation* entranceAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    entranceAnimation.duration = 0.5;
    entranceAnimation.fromValue = @1.5f;
    entranceAnimation.toValue = @1.0f;
    entranceAnimation.fillMode = kCAFillModeForwards;
    entranceAnimation.removedOnCompletion = NO;
    [_nikeLayer addAnimation:entranceAnimation forKey:nil];
    
    [imageView.layer addSublayer:_nikeLayer];
}

-(void)stopAnimation:(UIImageView*)imageView{
    
    [_nikeLayer removeFromSuperlayer];
    [imageView.layer removeAllAnimations];
}


@end
